<template>
	<view :class="{wrap0: tHeaderIndex == 0, wrap1: tHeaderIndex == 1}">
		<view class="tHeader">
			<g-header rtype="search" @rightclick="onSkipTap('takeOrderSearch')">
				<template v-slot:title>
					<view class="tHeader-nav">
						<view class="tHeader-nav_item" :class="{active: tHeaderIndex == 0}" @click="onTHeaderNavTap(0)">
							采购需求</view>
						<view class="tHeader-nav_item" :class="{active: tHeaderIndex == 1}" @click="onTHeaderNavTap(1)">
							已报价</view>
						<view class="tHeader-nav_border" :style="{left: `${46 + tHeaderIndex * 152}rpx`}"></view>
					</view>
				</template>
			</g-header>
			<scroll-view class="nav" :scroll-x="true" v-if="tHeaderIndex == 0">
				<view class="nav-item" :class="{active: categoryIndex == -1}" @click="onCategoryTap(-1)">全部</view>
				<view class="nav-item" :class="{active: categoryIndex == index}" v-for="(item, index) in categoryList"
					:key="index" @click="onCategoryTap(index)">{{item.name}}</view>
			</scroll-view>
		</view>
		<view class="list">
			<view class="item" v-for="(item, index) in list" :key="index" @click="onSkipTap('purchaseNote', index)">
				<block v-if="tHeaderIndex == 0">
					<view class="item-status" v-if="item.purchaseStatus == 0">进行中</view>
					<view class="item-status finished" v-if="item.purchaseStatus == 1">已报价</view>
					<view class="item-status finished" v-if="item.purchaseStatus == 2">已成交</view>
					<view class="item-status outOfDate" v-if="item.purchaseStatus == 3">已过期</view>
				</block>
				<block v-if="tHeaderIndex == 1">
					<view class="item-status" v-if="item.purchaseStatus == 1">报价中</view>
					<view class="item-status finished" v-if="item.purchaseStatus == 2">已成交</view>
					<view class="item-status outOfDate" v-if="item.purchaseStatus == 3">已过期</view>
				</block>
				<view class="item-img">
					<u-image width="350rpx" height="350rpx" :src="item.cover"></u-image>
				</view>
				<view class="item-title app_ellipsis2">{{item.productName}}</view>
				<view class="item-bottom">
					<view class="item-avatar">
						<u-avatar :src="item.avatar" size="40"></u-avatar>
					</view>
					<view class="item-phone">{{item.secretTel}}</view>
					<view class="item-date">{{item.createTime}}</view>
				</view>
			</view>
		</view>

		<u-popup mode="center" v-model="showVipPop" border-radius="10">
			<view class="vipPop">
				<!-- <icon class="vipPop-icon" type="clear" color="#ddd" size="22" @click="showVipPop = false"></icon> -->
				<view class="vipPop-img">
					<image src="../../static/images/icon-hg.png" mode=""></image>
				</view>
				<view class="vipPop-desc">需升级成为会员VIP后才可以接单报价</view>
				<view class="vipPop-btn">
					<g-btn title="马上开通" :customstyle="{width: '500rpx', height: '90rpx'}"
						@click="onSkipTap('vipCenter')"></g-btn>
				</view>
			</view>
		</u-popup>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				tHeaderIndex: 0,
				isVipFlag: false,
				showVipPop: false,
				list: [],
				page: 1,
				size: 15,
				hasLoad: false,
				noMore: false,
				categoryList: [],
				categoryIndex: -1,
			}
		},
		onLoad(options) {
			this.updatePage()
			this.updateVipFlag()
			uni.$on('update-takeOrder-vipFlag', this.updateVipFlag)
		},
		onUnload() {
			uni.$off('update-takeOrder-vipFlag', this.updateVipFlag)
		},
		onShow() {

		},
		onPullDownRefresh() {
			this.updatePage()
		},
		onReachBottom() {
			this.page++
			this.getList()
		},
		methods: {
			updateVipFlag() {
				this.$store.dispatch('updateUserInfo').then(userInfo => {
					// console.log(userInfo);
					if (userInfo && userInfo.vipExpireTime) {
						let timestamp = new Date(userInfo.vipExpireTime).getTime()
						let currentTimestamp = new Date().getTime()
						if (currentTimestamp < timestamp) {
							this.isVipFlag = true
						} else {
							this.showVipPop = true
						}
					} else {
						this.showVipPop = true
					}
				})
			},
			updatePage() {
				this.resetPage()
				this.onTHeaderNavTap(this.tHeaderIndex)
				this.getCategoryList()
			},
			resetPage() {
				this.list = []
				this.page = 1
				this.size = 15
				this.hasLoad = false
				this.noMore = false
			},
			onTHeaderNavTap(index) {
				this.tHeaderIndex = index
				this.resetPage()
				if (index == 0) {
					this.getList()
				} else if (index == 1) {
					this.getQuotedList()
				}
			},
			onSkipTap(type, params) {
				let urlObj = {
					takeOrderSearch: '/pages/takeOrder/takeOrderSearch',
					purchaseNote: () => {
						if (!this.isVipFlag) {
							this.showVipPop = true
							return
						}
						uni.navigateTo({
							url: `/pages/takeOrder/purchaseNote?purchaseId=${this.list[params].purchaseId}`
						})
					},
					vipCenter: '/pages/vip/center'
				}
				this.$g_util.navigationTo(urlObj, type)
			},
			/**
			 * 获取采购需求列表
			 */
			getList() {
				let category = 0
				if (this.categoryIndex != -1) {
					category = this.categoryList[this.categoryIndex].id
				}
				this.$g_api.purchaseList({
					category,
					pageNum: this.page,
					pageSize: this.size
				}).then(({
					data
				}) => {
					uni.stopPullDownRefresh()
					this.hasLoad = true
					if (data?.length > 0) {
						data.forEach(item => {
							item.createTimeStr = new Date(item.createTime).Format('yyyy-MM-dd')
							item.secretTel = item.tel.slice(0, 3) + '****' + item.tel.slice(7, 11)
							item.cover = this.$g_util.concatRequestUrl(item.cover)
							item.avatar = this.$g_util.concatRequestUrl(item.avatar)
						})
						this.list = this.list.concat(data)
					} else {
						this.noMore = true
					}
				})
			},
			getQuotedList() {
				this.$g_api.purchaseQuotedList({
					pageNum: this.page,
					pageSize: this.size
				}).then(({
					data
				}) => {
					uni.stopPullDownRefresh()
					this.hasLoad = true
					if (data?.length > 0) {
						data.forEach(item => {
							item.startTimeStr = new Date(item.startTime).Format('yyyy-MM-dd')
							item.secretTel = item.tel.slice(0, 3) + '****' + item.tel.slice(7, 11)
							item.cover = this.$g_util.concatRequestUrl(item.cover)
							item.avatar = this.$g_util.concatRequestUrl(item.avatar)
						})
						this.list = this.list.concat(data)
					} else {
						this.noMore = true
					}
				})
			},
			getCategoryList() {
				this.$g_api.purchaseCategoryList({
					pageNum: 1,
					pageSize: 999
				}).then(({
					data
				}) => {
					this.categoryList = data || []
				})
			},
			onCategoryTap(index) {
				this.categoryIndex = index
				this.onTHeaderNavTap(this.tHeaderIndex)
			}
		},
	}
</script>

<style scoped lang="scss">
	.wrap0 {
		padding-top: calc(var(--status-bar-height) + 98rpx + 90rpx)
	}

	.wrap1 {
		padding-top: calc(var(--status-bar-height) + 98rpx)
	}

	.tHeader {
		position: fixed;
		left: 0;
		top: 0;
		z-index: 9990;
	}

	.tHeader-nav {
		position: relative;
		display: flex;
	}

	.tHeader-nav_item {
		line-height: 98rpx;
		width: 152rpx;
		text-align: center;

		&.active {
			color: $g-main-color;
		}
	}

	.tHeader-nav_border {
		position: absolute;
		left: 46rpx;
		bottom: 10rpx;
		width: 60rpx;
		height: 5rpx;
		border-radius: 3rpx;
		background-color: $g-main-color;
		transition: left 0.3s;
	}

	.nav {
		width: 750rpx;
		height: 90rpx;
		padding: 20rpx 0;
		white-space: nowrap;
		box-sizing: border-box;
		background-color: #fff;
	}

	.nav-item {
		display: inline-block;
		// height: 50rpx;
		text-align: center;
		padding: 0 20rpx;
		line-height: 50rpx;
		margin-right: 20rpx;
		border-radius: 6rpx;
		background-color: #f2f2f2;

		&.active {
			color: $g-main-color;
		}

		&:first-child {
			margin-left: 30rpx;
		}

		&:last-child {
			margin-right: 30rpx;
		}
	}

	.list {
		display: flex;
		justify-content: space-between;
		flex-wrap: wrap;
		padding: 18rpx;
	}

	.item {
		position: relative;
		width: 350rpx;
		border-radius: 10rpx;
		padding-bottom: 70rpx;
		margin-bottom: 18rpx;
		overflow: hidden;
		background-color: #fff;
	}

	.item-status {
		position: absolute;
		left: 0;
		top: 0;
		width: 100rpx;
		height: 40rpx;
		display: flex;
		justify-content: center;
		align-items: center;
		border-radius: 10rpx 0px 20rpx 0px;
		font-size: 22rpx;
		color: #fff;
		background-color: #FF4C4C;
		z-index: 99;

		&.outOfDate {
			background-color: #999;
		}

		&.finished {
			background-color: #3377FF;
		}
	}

	.item-img {}

	.item-title {
		margin: 16rpx 16rpx 4rpx;
	}

	.item-bottom {
		position: absolute;
		bottom: 18rpx;
		left: 0;
		display: flex;
		align-items: center;
	}

	.item-avatar {
		font-size: 0;
		margin-left: 16rpx;
		margin-right: 10rpx;
	}

	.item-phone {
		margin-right: 30rpx;
		font-size: 20rpx;
	}

	.item-date {
		color: #999;
		font-size: 20rpx;
	}

	.vipPop {
		display: flex;
		flex-direction: column;
		align-items: center;
		position: relative;
		width: 600rpx;
		padding: 64rpx 0 50rpx;
	}

	.vipPop-icon {
		position: absolute;
		right: 20rpx;
		top: 20rpx;
	}

	.vipPop-img {
		image {
			width: 158rpx;
			height: 158rpx;
		}
	}

	.vipPop-desc {
		margin: 30rpx 0 70rpx;
		font-size: 30rpx;
	}

	.vipPop-btn {}
</style>